import logging

import pytest

from resource.pom.keyboard import Keyboard
from resource.pom.memorandum import Memorandum


class Test:
    app = Memorandum
    keyboard = Keyboard

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('预置条件.清除测试数据')
        device.hdc_shell('rm -rf /data/app/el2/100/database/com.ohos.note/default/rdb/note.db')
        device.wait(2)
        logging.info('步骤1.进入桌面，点击备忘录，新增笔记')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.click(*self.app.add_note)
        logging.info('预期结果1.可正常新增笔记')
        device.refresh_layout()
        device.assert_text_exist('标题')
        device.assert_type_exist('Web')
        web = device.get_element_by_type('Web')
        device.click_element(web)
        device.click(*self.keyboard.key_d)
        device.click(*self.keyboard.key_e)
        device.click(*self.keyboard.key_f)
        device.click(*self.keyboard.close)
        device.click(*self.app.save_note)
        device.refresh_layout()
        device.assert_text_exist('标题')
        # device.assert_text_exist('def')

        logging.info('步骤2.删除笔记')
        device.click(*self.app.del_note)
        device.refresh_layout()
        delete_dialog_btn = device.get_element_by_text('删除')
        device.click_element(delete_dialog_btn)
        logging.info('预期结果2.删除成功')
        device.refresh_layout()
        device.assert_text_exist('标题', expect=False)

        logging.info('步骤3.1.新增笔记')
        device.click(*self.app.add_note)
        device.click_element(web)
        device.click(*self.keyboard.key_d)
        device.click(*self.keyboard.key_e)
        device.click(*self.keyboard.key_f)
        device.click(*self.keyboard.close)
        device.click(*self.app.save_note)
        device.click(*self.app.back)
        logging.info('步骤3.2.长按笔记点击删除按钮，确认删除')
        device.refresh_layout()
        new_note = device.get_element_by_text('标题')
        device.long_click_element(new_note)
        device.click(*self.app.delete_menu)
        device.click_element(delete_dialog_btn)
        logging.info('预期结果3.删除成功')
        device.refresh_layout()
        device.assert_text_exist('标题', expect=False)

        logging.info('步骤4.进入最近删除，点击笔记，点击删除，确认删除')
        device.click(*self.app.open_menu)
        device.click(*self.app.recent_delete)
        device.click(*self.app.close_menu)
        device.refresh_layout()
        # 进入笔记里面删除
        note = device.get_element_by_text('标题')
        device.click_element(note)
        device.click(*self.app.delete_in_note_from_dustbin)
        device.refresh_layout()
        delete_btn = device.get_element_by_text('删除')
        device.click_element(delete_btn)
        # 最近删除的主页长按删除
        device.long_click_element(note)
        device.click(*self.app.delete_in_dustbin_main_page)
        device.click_element(delete_btn)
        logging.info('预期结果4.笔记永久删除，最近删除中笔记数量相应减少')
        device.refresh_layout()
        device.assert_text_exist('没有笔记')
        device.click(*self.app.open_menu)
        device.click(*self.app.all_note)
        device.click(*self.app.close_menu)
        device.refresh_layout()
        device.assert_text_exist('标题', expect=False)
